
/**
* 参数处理
* @param {*} params  参数
*/
export function tansParams (params) {
  let result = ''
  for (const propName of Object.keys(params)) {
    const value = params[propName]
    const part = encodeURIComponent(propName) + '='
    if (value !== null && value !== '' && typeof (value) !== 'undefined') {
      if (typeof value === 'object') {
        for (const key of Object.keys(value)) {
          if (value[key] !== null && value[key] !== '' && typeof (value[key]) !== 'undefined') {
            const params = propName + '[' + key + ']'
            const subPart = encodeURIComponent(params) + '='
            result += subPart + encodeURIComponent(value[key]) + '&'
          }
        }
      } else {
        result += part + encodeURIComponent(value) + '&'
      }
    }
  }
  return result
}

// 验证是否为blob格式
export function blobValidate (data) {
  return data.type !== 'application/json'
}

// 日期格式化
export function parseTime (time, pattern) {
  if (arguments.length === 0 || !time) {
    return null
  }
  const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
      time = parseInt(time)
    } else if (typeof time === 'string') {
      time = time.replace(new RegExp(/-/gm), '/').replace('T', ' ').replace(new RegExp(/\.[\d]{3}/gm), '')
    }
    if ((typeof time === 'number') && (time.toString().length === 10)) {
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
    if (result.length > 0 && value < 10) {
      value = '0' + value
    }
    return value || 0
  })
  return time_str
}

export function timestampToDate (timestamp) {
  if (timestamp === undefined) {
    return ''
  } else {
    // const date = new Date(timestamp)
    // const year = date.getFullYear()
    // const month = ('0' + (date.getMonth() + 1)).slice(-2) // 月份是从0开始的
    // const day = ('0' + date.getDate()).slice(-2)
    // return year + '-' + month + '-' + day
    const date = new Date(timestamp);
    const year = date.getFullYear();
    const month = (date.getMonth() + 1).toString().padStart(2, '0');
    const day = date.getDate().toString().padStart(2, '0');
    const hour = date.getHours().toString().padStart(2, '0');
    const minute = date.getMinutes().toString().padStart(2, '0');
    const second = date.getSeconds().toString().padStart(2, '0');
    return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
  }
}


export function timestampToDate_setFun (timestamp) {
  if (timestamp === undefined) {
    return ''
  } else {
    const date = new Date(timestamp)
    const year = date.getFullYear()
    const month = ('0' + (date.getMonth() + 1)).slice(-2) // 月份是从0开始的
    const day = ('0' + date.getDate()).slice(-2)
    return year + '-' + month + '-' + day
  }
}

export function getStatus(val){
  if (val === 0) {
    return '待跟进'
  } else if(val === 1){
    return '己成交'
  } else if(val === 2){
    return '在考虑'
  } else if(val === 3){
    return '己邀约待上门'
  } else if(val === 4){
    return '己沟通不需要'
  } else if(val === 5){
    return '己上门待签约'
  } else if(val === 6){
    return '己上门资质不服'
  } else if(val === 7){
    return '己成交'
  } else if(val === 8){
    return '进件'
  } else if(val === 9){
    return '成交'
  } else {
    return '-'
  }
}

// 防抖
export function debounce (fn, delay = 1000) {
  let timer = null
  return function () {
    if (timer) {
      clearTimeout(timer)
    }
    timer = setTimeout(() => {
      fn.apply(fn, arguments)
      timer = null
    }, delay)
  }
}

// 节流
export function throttle (fn, delay) {
  let last, timer
  return function () {
    const that = this
    const args = arguments
    const now = +new Date()
    if (last && now - last < delay) {
      clearTimeout(timer)
      timer = setTimeout(() => {
        last = now
        fn.apply(that, args)
      }, delay)
    } else {
      last = now
      fn.apply(that, args)
    }
  }
}




/**
 * 将日期对象格式化为指定格式的字符串
 * @param {Date|string} date - 日期对象或可转换为日期对象的字符串
 * @param {string} [format='YYYY-MM-DD'] - 输出格式，可选：
 *   YYYY-MM-DD (默认)
 *   YYYY/MM/DD
 *   YYYY年MM月DD日
 *   YY-MM-DD (两位年份)
 *   YYYY-MM-DD HH:mm:ss (带时间)
 * @returns {string} 格式化后的日期字符串
 */
export function formatChineseDate(date, format = 'YYYY-MM-DD') {
  // 处理输入为字符串或时间戳的情况
  const parsedDate = date instanceof Date ? date : new Date(date);
  
  // 检查日期是否有效
  if (isNaN(parsedDate.getTime())) {
    throw new Error('Invalid date input');
  }

  // 获取各个日期部分
  const year = parsedDate.getFullYear();
  const month = String(parsedDate.getMonth() + 1).padStart(2, '0');
  const day = String(parsedDate.getDate()).padStart(2, '0');
  const hours = String(parsedDate.getHours()).padStart(2, '0');
  const minutes = String(parsedDate.getMinutes()).padStart(2, '0');
  const seconds = String(parsedDate.getSeconds()).padStart(2, '0');

  // 根据指定格式返回结果
  return format
    .replace('YYYY', year)
    .replace('YY', String(year).slice(-2))
    .replace('MM', month)
    .replace('DD', day)
    .replace('HH', hours)
    .replace('mm', minutes)
    .replace('ss', seconds);
}
